<?php

class ArticleCategoriesController extends AppController {

    var $uses = array('ArticleCategory', 'Article');
    var $helpers = array('Html', 'Form', 'Session');
    var $components = array('Session');

    public function index() {
        $result = $this->ArticleCategory->find('all');
        $this->set('categories', $result);
    }

    public function show($id, $title) {
        $category = $this->ArticleCategory->find('first', array(
                    'conditions' => array(
                        "ArticleCategory.id = '" . $id . "'"
                    ),
                    'recursive' => '-1'
                ));
        $result = $this->Article->find('all', array(
                    'conditions' => array(
                        "ArticleCategory.id = '" . $id . "'"
                    )
                ));
        if ($result != false) {
            $this->title = $category['ArticleCategory']['name'];
            $this->set("category", $category['ArticleCategory']);
            $this->set("articles", $result);
        }
    }

    public function admin_index() {
        $this->title = "Kategorie artykułów";

        $result = $this->ArticleCategory->find('all');
        $this->set('categories', $result);
    }

    public function admin_view($id) {
        $category = $this->ArticleCategory->find('first', array(
                    'conditions' => array(
                        "ArticleCategory.id = '" . $id . "'"
                    ),
                    'recursive' => '-1'
                ));
        $result = $this->Article->find('all', array(
                    'conditions' => array(
                        "ArticleCategory.id = '" . $id . "'"
                    )
                ));
        $this->title = "Artykuły w kategorii: " . $category['ArticleCategory']['name'];
        $this->set("articles", $result);

        $this->set('category', $category['ArticleCategory']);
    }

    function admin_edit($id = null) {
        $this->title = "Edycja kategorii artykułów";

        $this->ArticleCategory->id = $id;
        if (empty($this->data)) {
            $this->data = $this->ArticleCategory->read();
        } else {
            if ($this->ArticleCategory->save($this->data)) {
                $this->Session->setFlash('Zapisano kategorię.');
                $this->redirect(array('action' => 'edit', $id));
            }
        }
    }

    public function admin_delete($id) {
        $this->title = "Usuwanie kategorii artykułów";

        if($id == 0) $this->trigger404();

        if ($this->ArticleCategory->delete($id)) {
            $this->Session->setFlash('Usunięto.');
            $this->redirect(array('action' => 'index'));
        }
    }

    public function admin_delete_from_category($categoryId, $articleId) {
        $this->title = "Usuwanie artykułu z kategorii";

        $this->Article->read(null, $articleId);
        $this->Article->set("category_id", 0);

        if ($this->Article->save()) {
            $this->Session->setFlash('Zapisano.');
            $this->redirect(array('action' => 'admin_view', $categoryId));
        }
    }

    public function admin_create() {
        $this->title = "Nowa kategoria artykułów";

        $this->helpers[] = "CKEditor";
        if (!empty($this->data)) {
            if ($this->ArticleCategory->save($this->data)) {
                $this->Session->setFlash('Zapisano stronę.');
                $this->redirect(array('action' => 'edit', $this->data['id']));
            }
        }
    }

}
?>
